package com.group.controller;


import com.group.entity.ProjectInside;
import com.group.service.impl.FileService;
import com.group.service.impl.ProjectInsideServiceImpl;
import com.group.service.impl.ProjectOutsideServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Controller
@RequestMapping("/pi")
public class ProjectInsideController {

    @Autowired
    private FileService fileService;

    @Autowired
    private ProjectInsideServiceImpl pis;

    @Autowired
    private ProjectOutsideServiceImpl pos;


    //写项目测试的时候用的
    @RequestMapping("/")
    public String toTwoxingxi(){
        return "twoxingxi";
    }

    //往广场项目表和具体项目信息表中添加信息,然后跳转至xieyi这个页面
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String addOneMessage(@RequestParam("file") MultipartFile file, ProjectInside project, HttpSession session)
    {
        Integer currentUserId = (Integer) session.getAttribute("currentUserId");
        //仅仅获得文件的名称,并没有真正地上传
        String fileName = fileService.storeFile(file);
        //在Outside表中添加一行数据后,返回项目编号
        int projectId = pos.addInProjectOutside(project,currentUserId);
        if(pis.addMessage(projectId,fileName, project)>0){
            System.out.println("添加成功");
        }
        return "xieyi";
    }


    //返回项目的详细信息
    @RequestMapping("/show")
    @ResponseBody
    public ProjectInside selectProjectByGroupId (String groupId){
        return pis.selectProjectByGroupId(Integer.parseInt(groupId));
    }

}

